<?php


namespace app\api\controller\v25;


use app\aliyun\controller\AliyunFilterWord;
use app\api\controller\v2\Auth;
use app\common\cachemodel\AppSynastryResult;
use app\common\cachemodel\chat\HzChatLastSendTimeRecord;
use app\common\cachemodel\chat\HzChatRecord;
use app\common\cachemodel\Recode as RecodeCacheModel;
use app\common\lib\astrolog\Astrolog;
use app\common\lib\SensorsData;
use app\common\model\User;
use think\Cache;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Log;

class Recode extends Auth
{
    protected $setting = [
        'natal' => [
            'planet' => [
                [1, 8.5], [1, 7.5], [1, 3.5], [1, 3.5], [1, 4], [1, 4.5], [1, 4.5], [1, 2.5], [1, 2.5], [1, 2.5],
                [0, 0], [0, 0], [0, 0], [0, 0], [1, 2.5], [0, 0], [0, 0], [1, 2.5], [1, 2.5], [1, 2.5], [1, 2.5],
            ],
            'phase' => [
                0 => [1, 0], 60 => [1, 0], 90 => [1, 0], 120 => [1, 0], 180 => [1, 0],
                45 => [0, 0], 135 => [0, 0], 30 => [0, 0], 150 => [0, 0], 72 => [0, 0], 144 => [0, 0],
            ],
            'arabic' => [
                [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
            ],
            'h_sys' => 'p',
            'firdaria' => 'default',
            'bound_type' => 'default',
            'ecliptic' => 0,
            'profection_push_direction' => 0,
            'decanates' => 0,
            'ayanamsa' => 0,
            'location_use' => 0,
        ],
    ];

    protected $noNeedJudgeSummer = ['香港特别行政区', '澳门特别行政区', '台湾省', '北美洲', '大洋洲', '非洲', '南美洲', '欧洲', '亚洲', ''];

    public function update()
    {
        $data = [
            'id' => input('id/d'),// 档案id
            'uid' => $this->user['id'],// 用户id
            'name' => input('name/s', '', 'htmlspecialchars'),// 档案名称
            'type' => input('type/d'),// 档案类型  1男 | 2 女 | 3 男同 | 4 女同 | 5 事件 | 6 卜卦
            'year' => input('year/d'),// 出生时间-年
            'month' => input('month/d'),// 出生时间-月
            'day' => input('day/d'),// 出生时间-日
            'hour' => input('hour/d'),// 出生时间-时
            'minute' => input('minute/d'),// 出生时间-分
            'timezone' => input('timezone/d', 8),// 出生时间-时区
            'birth_country' => input('birth_country/s', '', 'htmlspecialchars'),// 出生地点-省
            'birth_province' => input('birth_province/s', '', 'htmlspecialchars'),// 出生地点-市
            'birth_city' => input('birth_city/s', '', 'htmlspecialchars'),// 出生地点-区
            'live_country' => input('live_country/s', '', 'htmlspecialchars'),// 现居地-省
            'live_province' => input('live_province/s', '', 'htmlspecialchars'),// 现居地-省
            'live_city' => input('live_city/s', '', 'htmlspecialchars'),// 现居地-省
            'comments' => input('comments/s', '', 'htmlspecialchars'),// 档案标签如 自己 朋友 案例
            'isself' => input('isself/d', 0),// 档案是否为自己 0 否 | 1 是（自己的档案无法删除，无法更改为其他标签）
            'long_deg' => input('long_deg/d'),// 经度-度
            'long_min' => input('long_min/d'),// 经度-分
            'ew' => input('ew/d'),// 东经|西经 1|-1
            'lat_deg' => input('lat_deg/d'),// 纬度-度
            'lat_min' => input('lat_min/d'),// 纬度-度
            'ns' => input('ns/d'),// 南纬北纬  1|-1
            'avatar' => input('avatar/s', '', 'htmlspecialchars'),// 头像地址
            'status' => 1,// 档案状态
            'from' => 'zhunle',// 档案来源
            'is_summer' => 0, // 默认为不开启夏令时
        ];
        $data['name'] = str_replace(["\r\n", "\r", "\n"], "", $data['name']);
        // 只判断大陆地区的夏令时
        if (!in_array($data['birth_country'], $this->noNeedJudgeSummer, true))
        {
            $data['is_summer'] = isSummerTime($data['hour'], $data['minute'], 0, $data['month'], $data['day'], $data['year']) ? 1 : 0;// 是否开启夏令时
        }

        $birth_longitude = $data['ew'] * $data['long_deg'] + number_format($data['long_min'] / 60, 3);
        $birth_latitude = $data['ns'] * $data['lat_deg'] + number_format($data['lat_min'] / 60, 3);

        $referName = input('refer_name/s', '');
        $referLink = input('refer_link/s', '');

        $birthDateTime = date('Y-m-d H:i:s', strtotime($data['year'] . '-' . $data['month'] . '-' . $data['day'] . ' ' . $data['hour'] . ':' . $data['minute'] . ':00'));
        $sensorsTrackData = [
            'refer_name' => $referName,
            'refer_link' => $referLink,
            'record_id' => (int)$data['id'],
            'record_name' => $data['name'],
            'record_tag' => $data['comments'],
            'record_sex' => $data['type'] === 1 ? '男' : '女',
            'birth_time' => $birthDateTime,
            'is_summer' => $data['is_summer'] === 1,
            'birth_country' => $data['birth_country'],
            'birth_province' => $data['birth_province'],
            'birth_city' => $data['birth_city'],
            'birth_longitude' => $birth_longitude,
            'birth_latitude' => $birth_latitude,
            'live_country' => $data['live_country'],
            'live_province' => $data['live_province'],
            'live_city' => $data['live_city'],
            'add_type' => '手动',
            'record_avatar' => $data['avatar'],
            'is_have_avatar' => $data['avatar'] !== '',
            'is_success' => true,
            'failed_reson' => '',
            'handle_name' => '更新',// 更新档案方式   修改、新建、更新
        ];

        if (($data['isself'] === 1 && $data['comments'] !== '自己') || ($data['comments'] === '自己' && $data['isself'] !== 1))
        {
            $sensorsTrackData['is_success'] = false;
            $sensorsTrackData['failed_reson'] = '不满足isself为1且comment为自己';
            SensorsData::track($this->user['id'], true, 'RecordAdd', $sensorsTrackData);
            return show(config('code.error'), '服务异常，请稍后重试');
        }

        $RecodeCacheModel = new RecodeCacheModel();

        $validate = validate('Recode');

        if (!$validate->scene('edit')->check($data))
        {
            $sensorsTrackData['is_success'] = false;
            $sensorsTrackData['failed_reson'] = (string)$validate->getError();
            SensorsData::track($this->user['id'], true, 'RecordAdd', $sensorsTrackData);
            return show(config('code.error'), (string)$validate->getError());
        }

        $recodeModel = new RecodeCacheModel();
        // 查询未更改前的档案
        $recode_old = $recodeModel->getInfoById($data['id']);

        if (!$recode_old || $recode_old['uid'] != $this->user['id'] || !$recode_old['status'])
        {
            $sensorsTrackData['is_success'] = false;
            $sensorsTrackData['failed_reson'] = '原档案未找到';
            SensorsData::track($this->user['id'], true, 'RecordAdd', $sensorsTrackData);
            return show(config('code.error'), '档案不存在');
        }

        if ($recode_old['isself'] != 1 && $data['isself'] === 1 && ($this->user['rid'] && $this->user['rid'] != $data['id']))
        {
            $sensorsTrackData['is_success'] = false;
            $sensorsTrackData['failed_reson'] = '自己的档案只能有一个';
            SensorsData::track($this->user['id'], true, 'RecordAdd', $sensorsTrackData);
            return show(config('code.error'), '不能添加多个自己的档案');
        }

        // 检测档案编辑是否更改时间
        $timeChange = false;// 默认表示时间未进行更新
        $old_time = strtotime($recode_old['year'] . '-' . $recode_old['month'] . '-' . $recode_old['day'] . ' ' . $recode_old['hour'] . ':' . $recode_old['minute']);
        $new_time = strtotime($data['year'] . '-' . $data['month'] . '-' . $data['day'] . ' ' . $data['hour'] . ':' . $data['minute']);
        // 时间有变动
        if ($old_time != $new_time || $recode_old['timezone'] != $data['timezone'] || $recode_old['is_summer'] != $data['is_summer'])
        {
            $timeChange = true;

            // if (Util::getVips($this->user['id'], 0, 0))
            // {
            //     $rec = false;
            // } // 检测是否存在档案相关已付款订单
            // else
            // {
            //     $rec = RecodeCacheModel::checkAboutOrders(['uid' => $data['uid'], 'id' => $data['id']]);
            // }
            //
            // // 若有，则档案时间只能为当天内变动，若跨日期，则只能在两小时以内变动
            // if ($rec === true)
            // {
            //     // 判断or_day是否存在
            //     if (empty($recode_old['or_day']) || !$recode_old['or_day'])
            //     {
            //         $recode_old['or_day'] = $recode_old['day'];
            //         $data['or_day'] = $recode_old['day'];
            //     }
            //
            //     $limit_time = strtotime($recode_old['year'] . '-' . $recode_old['month'] . '-' . $recode_old['or_day'] . ' 12:00');
            //     $max = $limit_time + 3600 * 14;
            //     $min = $limit_time - 3600 * 14;
            //
            //     if ($new_time > $max || $new_time < $min)
            //     {
            //         return show(config('code.error'), '由于该档案购买过付费项目，修改功能受限，改后时间不能与原档案时间差超过两个小时');
            //     }
            // }
        }
        // 检测档案编辑是否更改出生地经纬度
        $old_longitude = ($recode_old['long_deg'] + $recode_old['long_min'] / 60) * $recode_old['ew'];
        $old_latitude = ($recode_old['lat_deg'] + $recode_old['lat_min'] / 60) * $recode_old['ns'];
        $new_longitude = ($data['long_deg'] + $data['long_min'] / 60) * $data['ew'];
        $new_latitude = ($data['lat_deg'] + $data['lat_min'] / 60) * $data['ns'];

        $longLatChange = false;

        if ($old_longitude != $new_longitude || $old_latitude != $new_latitude)
        {
            $longLatChange = true;
        }
        try
        {
            // 查询当前星盘日月水金火木土等星落座落宫存入数据库
            $astro_data = Astrolog::getInstance()->setExecPrefix(config('astrolog.prefix'))
                ->setIsAstrolabe(false)
                ->setRecode1($data)
                ->setUserSetting($this->setting)
                ->getTheRecodeAstro();

            // 仅检测自己的档案，其他人的档案由于其他人看不到，所以不校验
            if ($data['isself'] === 1)
            {
                $res = AliyunFilterWord::checkText($data['name'], 'username');
                //若内容有违规且超过配置的相似比例，则进行隐藏不显示，将标签存入库中
                if ($res)
                {
                    $sensorsTrackData['is_success'] = false;
                    $sensorsTrackData['failed_reson'] = '昵称涉嫌' . $res['label'] . '，请重新设置';
                    SensorsData::track($this->user['id'], true, 'RecordAdd', $sensorsTrackData);
                    return show(0, '昵称涉嫌' . $res['label'] . '，请重新设置');
                }
            }
            $data['sun'] = $astro_data['sun'];
            $data['moon'] = $astro_data['moon'];
            $data['asc'] = $astro_data['asc'];
            $data['merc'] = $astro_data['merc'];
            $data['venu'] = $astro_data['venu'];
            $data['mars'] = $astro_data['mars'];
            $data['jupi'] = $astro_data['jupi'];
            $data['satu'] = $astro_data['satu'];
            $data['non'] = $astro_data['non'];
            $data['lili'] = $astro_data['lili'];
            $data['juno'] = $astro_data['juno'];
            $data['mc'] = $astro_data['mc'];
            // 更新档案数据
            $RecodeCacheModel->UpdateData($this->user['id'], $data['id'], $data);
            // 如果更新的是自己的档案 且 用户修改了档案名称或头像 则更新用户数据 昵称 头像
            if ($recode_old['isself'] == 1 && ($data['name'] != $recode_old['name'] || $data['avatar'] != $recode_old['avatar']))
            {
                (new User())->updateUserinfo(['id' => $data['uid']], ['nickname' => $data['name'], 'avatar' => $data['avatar']]);
            } elseif ($recode_old['isself'] != 1 && $data['isself'] == 1)
            {  // 如果旧档案不是自己 改为了自己
                (new User())->updateUserinfo(['id' => $data['uid']], ['nickname' => $data['name'], 'avatar' => $data['avatar']]);
            }
            // 更新档案行星数据
            if ($timeChange || $longLatChange)
            {
                // 记录用户更新档案的时间
                Db::table('app_recode_info')->where('rid', $data['id'])->update(['last_time' => time()]);
                // 更新用户档案档案信息 行星落座信息
                Db::table('app_recode_astro')->where(['id' => $recode_old['aid']])->update($astro_data);
            }
            $data['aid'] = $recode_old['aid'];

            // 档案更新时更新用户档案缓存
            Cache::set('recode_' . $data['id'], $data, 86400 * 7);
            // 档案更新时更新用户档案日月升缓存
            Cache::rm('recode_astro_' . $data['id']);
            //删除首页用户星座缓存
            Cache::rm('d_f_h_sun_in_sign_' . $data['id']);

            // if ($this->user['rid'] == $data['id'])
            // {
            // 2.6.0编辑档案不更新日运信息
            // FortuneCache::rmFortuneDayCache($data['id'], strtotime(date('Y-m-d') . ' 12:00:00'));
            // }
        }
        catch (\Exception $e)
        {
            Log::write('编辑档案失败：' . $e->getMessage(), 'edit_recode_error', true);
            $sensorsTrackData['is_success'] = false;
            $sensorsTrackData['failed_reson'] = $e->getMessage();
            SensorsData::track($this->user['id'], true, 'RecordAdd', $sensorsTrackData);
            return show(config('code.error'), '更新失败');
        }

        //将档案购买的年运订单重新加入队列
//        $this->fortune_redis_job(['rid'=>$data['id'],'uid'=>$data['uid']]);
        $sensorsTrackData['is_success'] = true;
        $sensorsTrackData['failed_reson'] = '';
        SensorsData::track($this->user['id'], true, 'RecordAdd', $sensorsTrackData);
        // 若成功更新自己的档案，则更新用户属性
        if ($data['isself'] === 1)
        {
            $userData = [
                'user_nickname' => $sensorsTrackData['record_name'],// 昵称	STRING	我是一个昵称示例
                'user_sex' => $sensorsTrackData['record_sex'],// 性别	STRING	男、女
                'user_birth_time' => $sensorsTrackData['birth_time'],// 出生时间	DATETIME	2021-03-22 11:11:00
                'user_birth_province' => $sensorsTrackData['birth_country'],// 出生省份	STRING	北京市
                'user_birth_city' => $sensorsTrackData['birth_province'],// 出生城市	STRING	北京市
                'user_birth_area' => $sensorsTrackData['birth_city'],// 出生地区	STRING	朝阳区
                'user_live_province' => $sensorsTrackData['live_country'],// 现居省份	STRING	北京市
                'user_live_city' => $sensorsTrackData['live_province'],// 现居城市	STRING	北京市
                'user_live_area' => $sensorsTrackData['live_city'],// 现居地区	STRING	朝阳区
                'user_is_summer' => $sensorsTrackData['is_summer'],// 是否出生在夏令时	BOOL	true、false
                'user_birth_longitude' => $sensorsTrackData['birth_longitude'],// 出生地经度	NUMBER	120.56
                'user_birth_latitude' => $sensorsTrackData['birth_latitude'],// 出生地纬度	NUMBER	35.42
            ];
            SensorsData::profile($this->user['id'], true, $userData, 'set');

            $hzChatRecordModel = new HzChatRecord();
            $chatId = $hzChatRecordModel->getChatIdByUid($this->user['id']);
            if ($chatId)
            {   // 更新当前档案 为自己的档案 如果当前 用户还有对话记录档案ID为0的聊天记录【添加档案前的聊天记录】 则删除当前聊天记录
                $hzChatRecordModel->delChat($this->user['id'], 0, $chatId);
            }
        }

        //返回is_read=1写死【Android需要返回】（0：未读，显示new；1：已读）
        $data['is_read'] = 1;
        return show(config('code.success'), '更新成功', array_merge($data, $astro_data));
    }

    /**
     * 删除档案
     * @return mixed
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function delete()
    {
        $id = input('id/d', 0);// 档案id

        $uid = $this->user['id'];

        $validate = validate('Recode');

        if (!$validate->scene('del')->check(['id' => $id, 'uid' => $uid]))
        {
            SensorsData::track($this->user['id'], true, 'RecordDelete', [
                'record_id' => (int)$id,
                'record_name' => '',
                'is_success' => false,
                'failed_reson' => (string)$validate->getError(),
            ]);
            return show(config('code.error'), (string)$validate->getError());
        }
        $recodeModel = new RecodeCacheModel();
        // 查询档案
        $data = $recodeModel->getInfoById($id);

        if (empty($data) || !$data['status'])
        {
            SensorsData::track($this->user['id'], true, 'RecordDelete', [
                'record_id' => (int)$id,
                'record_name' => '',
                'is_success' => false,
                'failed_reson' => '未找到该档案',
            ]);
            return show(config('code.success'), '档案不存在');
        } elseif ($data['uid'] != $this->user['id'])
        {
            SensorsData::track($this->user['id'], true, 'RecordDelete', [
                'record_id' => (int)$id,
                'record_name' => $data['name'],
                'is_success' => false,
                'failed_reson' => '不能删除属于他人的档案',
            ]);
            return show(config('code.success'), '档案不存在');
        }

        try
        {
            // 检测档案是否有合盘记录--并给记录做标记
            (new AppSynastryResult())->recodeDel($id, $uid);
            // 删除档案在档案袋中的信息
            Db::table('app_recode_cat')->where('rid', $id)->delete();
            // 删除档案
            $recodeModel->DelData($uid, $id, ['status' => 0], $data['isself']);

            if ($data['isself'] == 1)
            {   // 自己的档案
                (new \app\common\cachemodel\User())->where('id', $this->user['id'])->update(['has_self_recode' => 0]);
                Cache::rm('userinfo:' . $this->user['id']); // 删除用户缓存
                // 当前自己的档案为 0  且 会话ID 也为0
                (new HzChatLastSendTimeRecord())->UpdateData($this->user['id'], ['rid' => 0, 'chat_id' => 0]);
            }
            // 档案获取chat_id 然后删除 会话记录
            $hzChatRecordModel = new HzChatRecord();
            $chatId = $hzChatRecordModel->getChatIdByRid($id);
            if ($chatId)
            {
                $hzChatRecordModel->delChat($this->user['id'], $id, $chatId);
            }
            // 删除档案缓存
            Cache::rm('recode_' . $id);
            Cache::rm('recode_lists_' . $uid);
            // 删除档案地点收藏缓存
            Cache::rm('map_collect_rids1_' . $uid);
        }
        catch (\Exception $e)
        {
            SensorsData::track($this->user['id'], true, 'RecordDelete', [
                'record_id' => (int)$id,
                'record_name' => $data['name'],
                'is_success' => false,
                'failed_reson' => $e->getMessage(),
            ]);
            return show(config('code.error'), '删除失败');
        }
        SensorsData::track($this->user['id'], true, 'RecordDelete', [
            'record_id' => (int)$id,
            'record_name' => $data['name'],
            'is_success' => true,
            'failed_reson' => '',
        ]);

        return show(config('code.success'), '删除成功');
    }
}
